package com.mypan.uitls;


import com.mypan.configure.FileConfigLoader;
import com.mypan.dao.tbFile;
import com.mypan.service.FileService;
import com.mypan.service.impl.FileServiceImpl;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Properties;
import java.util.UUID;

import org.apache.log4j.Logger;

/**
 * @author <a href="Tastill@**.cn">Tastill</a>
 * @version 2019/1/24 14:55
 * @description FileListener
 */
public class FileListener extends FileAlterationListenerAdaptor {
    public static final Logger logger = Logger.getLogger(FileListener.class);
    @Autowired
    FileConfigLoader fileConfigLoader;
    @Autowired
    PictureCompress pictureCompress;
    FileService fileService = new FileServiceImpl();

    /**
     * @param
     * @return
     * @description 启动监听
     * @version 2.0, 2019/1/24 15:08
     * @author <a href="Tastill@**.cn">Tastill</a>
     */
    @Override
    public void onStart(FileAlterationObserver observer) {
//        System.out.println("启动监听器：");
    }

    /**
     * @param
     * @return
     * @description 文件创建
     * @version 2.0, 2019/1/24 14:59
     * @author <a href="Tastill@**.cn">Tastill</a>
     */
    @SneakyThrows
    @Override
    public void onFileCreate(File file) {
        logger.info("有新文件生成：" + file.getName());
        logger.info("路径：" + file.getPath());
//        文件重命名
        UUID uuid = UUID.randomUUID();
        String extrName = file.getName().substring(file.getName().lastIndexOf(".") + 1);

        if (extrName.equals("png") ||
                extrName.equals("jpg") ||
                extrName.equals("jpeg") ||
                extrName.equals("webp") ||
                extrName.equals("gif") ||
                extrName.equals("svg") ||
                extrName.equals("ico")) {
//           若是压缩文件夹里的变动，则不进行压缩和信息记录
            if (!file.getPath().startsWith(pictureCompress.compressDir)) {
                boolean isCompress = pictureCompress.pictureCompress(file.getPath(), pictureCompress.compressDir + "\\" + uuid + '.' + extrName, 1024 * 100L, 0.5);
                tbFile tbFile = new tbFile(uuid.toString(), file.getName(), isCompress ? pictureCompress.compressDir + "\\" + uuid + '.' + extrName : "");
                fileService.alertImg(tbFile);
            }
        }
        File oldFile = new File(file.getPath());
        File newFile = new File(file.getPath().substring(0,
                file.getPath().lastIndexOf("/") + 1) + uuid + "." + extrName);
        FileUtils.moveFile(oldFile,newFile);

    }

    /**
     * @param
     * @return
     * @description 文件被删除
     * @version 2.0, 2019/1/24 16:13
     * @author <a href="Tastill@**.cn">Tastill</a>
     */
    @Override
    public void onFileDelete(File file) {
        logger.info("有文件被删除：" + file.getName());
        logger.info("路径：" + file.getPath());
        String extrName = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        if (extrName.equals("png") ||
                extrName.equals("jpg") ||
                extrName.equals("jpeg") ||
                extrName.equals("webp") ||
                extrName.equals("gif") ||
                extrName.equals("svg") ||
                extrName.equals("ico")
        ) {
            File f = new File(pictureCompress.compressDir + "/" + file.getName());
            if (f.exists()) {
                f.delete();
            }
        }
    }

    @Override
    public void onDirectoryCreate(File directory) {
//        logger.info("有新文件夹生成："+directory.getName());
//        logger.info("路径："+directory.getPath());
    }

    @Override
    public void onDirectoryChange(File directory) {
//        logger.info("有文件夹内容发生变化："+directory.getName());
//        logger.info("路径："+directory.getPath());
    }

    @Override
    public void onDirectoryDelete(File directory) {
//        logger.info("有文件夹被删除："+directory.getName());
//        logger.info("路径："+directory.getPath());
    }

    /**
     * @param
     * @return
     * @description 文件内容发生变化
     * @version 2.0, 2019/1/24 15:05
     * @author <a href="Tastill@**.cn">Tastill</a>
     */
    @Override
    public void onFileChange(File file) {
//        logger.info("有文件被修改："+file.getName());
//        logger.info("路径："+file.getPath());

    }

    /**
     * @param
     * @return
     * @description 监听停止
     * @version 2.0, 2019/1/24 15:07
     * @author <a href="Tastill@**.cn">Tastill</a>
     */
    @Override
    public void onStop(FileAlterationObserver observer) {
//        System.out.println("监听停止");
    }
}